import * as actionType from "./actionType"
import {loginApi} from "../../http/userApi"

export const loginAction = (user) => ({
  type: actionType.USER_LOGIN,
  user,
})
export const logoutAction = (user) => ({
  type: actionType.USER_LOGOUT,
  user,
})

//异步登录

export const loginAsyncAction = (user) => (dispatch) =>
  loginApi(user)
    .then((res) => {
      console.log('异步成功',res)
      if (res.data.code === 200) {
        dispatch(
          loginAction({
            ...res.data,
          })
        )
        return Promise.resolve(res)
      } else {
        return Promise.reject(res.data)
      }
    })
    .catch((err) => {
      return Promise.reject(err)
    })